﻿namespace Mp3Observer.Common.Language
{
    #region usings

    using System;
    using System.Windows;
    using System.Windows.Markup;

    #endregion

    /// <summary>
    /// This is a xaml markupextenison to provide a string from the langaugeservice with help of the <see cref="LanguageConverter"/>
    /// for any dependency property on any dependencyobject.
    /// </summary>
    [MarkupExtensionReturnType(typeof(string))]
    public sealed class LanguageExtension : MarkupExtension
    {
        #region Fields

        /// <summary>
        /// the name of the resource
        /// </summary>
        private string resourceName;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="LanguageExtension"/> class.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        public LanguageExtension(string resourceName)
        {
            this.resourceName = resourceName;
        }

        #endregion

        #region override ProvideValue

        /// <summary>
        /// This method either gets the resourcestring directly from the <see cref="LanguageConverter"/> or
        /// if its a depprop on a depo it makes a binding on the property Culture on our LanguageContext
        /// </summary>
        /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
        /// <returns>
        /// The object value to set on the property where the extension is applied.
        /// </returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            DependencyProperty dependencyProperty = null;
            DependencyObject targetObject = null;

            bool parameterOk = true;
            if (service == null)
                parameterOk = false;
            else
            {
                dependencyProperty = service.TargetProperty as DependencyProperty;
                if (dependencyProperty == null)
                    parameterOk = false;
                else
                {
                    targetObject = service.TargetObject as DependencyObject;
                    // if null, its a private System.Windows.SharedDp , thats the markupextension itself. So return this, then it works again when the template is applied.
                    // http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2802194&SiteID=1
                    if (targetObject == null)
                        return this;
                }
            }

            if (!parameterOk)
                return new LanguageConverter(this.resourceName).Convert(null, typeof(string), null, LanguageContext.Instance.Culture);
            else
                this.DoBinding(targetObject, dependencyProperty);
            return targetObject.GetValue(dependencyProperty);
        }

        /// <summary>
        /// Does the binding from target object to dependency property.
        /// </summary>
        /// <param name="targetObject">The target object.</param>
        /// <param name="dependencyProperty">The dependency property.</param>
        public void DoBinding(DependencyObject targetObject, DependencyProperty dependencyProperty)
        {
            LanguageContext.Instance.DoBinding(new LanguageConverter(this.resourceName), targetObject, dependencyProperty);
        }

        #endregion
    }
}
